package com.ycxy.sh.transaction.service.impl;

import com.ycxy.sh.transaction.dao.entity.Notices;
import com.ycxy.sh.transaction.dao.vo.LayuiPageVo;
import com.ycxy.sh.transaction.ex.ServiceException;
import com.ycxy.sh.transaction.mapper.NoticesMapper;
import com.ycxy.sh.transaction.service.NoticesService;
import com.ycxy.sh.transaction.web.JsonResult;
import com.ycxy.sh.transaction.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  消息通知服务类
 * </p>
 *
 * @author hlt
 * @since 2019-12-25
 */
@Slf4j
@Service
@Transactional
public class NoticesServiceImpl implements NoticesService {
    @Autowired
    private NoticesMapper noticesMapper;

    /**发出通知消息*/
    public void insertNotices(Long id){
        Integer i = noticesMapper.updateById(id);
        if (i != 1){
            String message="通知消息设置失败";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }
    }
    /**用户已读通知消息*/
    public int updateNoticesById(Long id){
        return noticesMapper.updateById(id);
    }
    /**查询前10条通知*/
    public List<Notices> queryNotices(HttpSession session){
        Long userid = (Long) session.getAttribute("userid");
        List<Notices> noticesList = noticesMapper.selectNotices(userid);
        if(noticesList==null){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"查询失败");
        }
        return noticesList;
    }
    /**取消新通知标志*/
    public void CancelLatest(HttpSession session){
        Long userid = (Long) session.getAttribute("userid");
        Integer row = noticesMapper.updateLatest(userid);
        if (row != 1){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"取消新通知标志失败");
        }
    }
    /**分页查询用户所有通知消息*/
    public List<Notices> pageUserAllNotices(Integer page, Integer count, Long userid){
        //分页查询用户所有通知消息
        List<Notices> noticesList = noticesMapper.queryAllNotices(page,count,userid);
        return noticesList;
    }

    //查询用户所有通知消息的数量
    public Integer queryNoticesCount(Long userid){
        Integer dataNumber = noticesMapper.Count(userid);
        return dataNumber;
    }
}
